package com.my.screenadapter.lifecircle;

import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.my.screenadapter.R;

/**
 * create by zyj 2017-12-19 15:19:22
 *
 * http://www.jianshu.com/p/dbc7e81aead2
 * https://segmentfault.com/a/1190000006691830
 * <p>
 * 目的：测试横竖屏 临时保存和生命周期
 * <p>
 * <p>
 * 结果：
 * 非重启Activity模式：即设置了android:configChanges="orientation|keyboardHidden|screenSize"，Log输出（横竖屏切换一样）
 * <p>
 * 11-03 10:38:33.998 20317-20317/com.ys.yoosir.screenconfigchange D/MainActivity: 被点击了
 * 11-03 10:38:34.108 20317-20317/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onConfigurationChanged
 * 重启Activity模式：即没有设置android:configChanges="orientation|keyboardHidden|screenSize"，Log输出（横竖屏切换一样）
 * <p>
 * 11-03 10:59:23.268 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity: 被点击了
 * 11-03 10:59:23.368 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onPause
 * 11-03 10:59:23.368 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onSaveInstanceState
 * 11-03 10:59:23.368 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onStop
 * 11-03 10:59:23.368 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onDestroy
 * 11-03 10:59:23.428 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onCreate - orientation
 * 11-03 10:59:23.458 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onStart
 * 11-03 10:59:23.458 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onRestoreInstanceState
 * 11-03 10:59:23.458 7724-7724/com.ys.yoosir.screenconfigchange D/MainActivity:  -- onResume
 */
public class SaveTempDataActivity extends AppCompatActivity {
    private static final String TAG = "SaveTempDataActivit";
    private Button btnScreen;
    private TextView tv1;
    private EditText et1;
    private int mCurrentIndex ;
    private static final String KEY_INDEX = "index";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate: ");
        setContentView(R.layout.activity_save_temp_data);
        btnScreen= (Button) findViewById(R.id.btn_screen);
        tv1= (TextView) findViewById(R.id.tv1);
        et1= (EditText) findViewById(R.id.et1);
        //是否已经 add 了 Fragment
        if(getSupportFragmentManager().findFragmentByTag("child") == null) {
            Log.d(TAG, " -- onCreate has no child ");
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            BlankFragment  blankFragment = BlankFragment.newInstance("one", "two");
            fragmentTransaction.add(R.id.root_view, blankFragment, "child");
            fragmentTransaction.commit();
        }else{
            Log.d(TAG, " -- onCreate has child ");
        }

        if (savedInstanceState != null) {
            Log.i(TAG, "onCreate: savedInstanceState    before==="+mCurrentIndex);
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
            Log.i(TAG, "onCreate: savedInstanceState    after==="+mCurrentIndex);
        }


    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.i(TAG, "onRestoreInstanceState: ");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState: ");
        outState.putInt(KEY_INDEX, mCurrentIndex);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //配置 android:configChanges="orientation|keyboardHidden|screenSize" 后  mCurrentIndex 没有改变
        Log.i(TAG, "onConfigurationChanged: ---mCurrentIndex==="+mCurrentIndex);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mCurrentIndex =111;
        Log.i(TAG, "onStart: ");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.i(TAG, "onPause: ");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy: ");
    }

    /**
     * 判断是否平板设备
     * @param context
     * @return true:平板,false:手机
     */
    private boolean isTabletDevice(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
    }

    /**
     * 横竖屏切换
     *
     * @param view
     */
    public void changeScreenOrientation(View view) {
        Log.i(TAG, "changeScreenOrientation: 按钮点击");
        //判断当前屏幕方向
        if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            //切换竖屏
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//竖屏
        } else {
            //切换横屏
            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);//API 9 以上，反向横屏
//            this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);//API 9 以上，反向竖屏
        }


    }


}
